﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;

using GA.Utils;

namespace HQ.Web.Handlers
{
    /// <summary>
    /// Summary description for DataTablesHandler
    /// </summary>
    public class DataTablesHandler : BaseHandler
    {
        public string sEcho { get; set; }
        public int iColumns { get; set; }
        public string sColumns { get; set; }
        public int iDisplayStart { get; set; }
        public int iDisplayLength { get; set; }
        public string sSearch { get; set; }
        public bool bRegex { get; set; }

        public int iSortingCols { get; set; }
        public int iSortCol_0 { get; set; }
        public string sSortDir_0 { get; set; }
        public bool bSortable_0 { get; set; }

        public PageDataTablesHelper PageInfo = new PageDataTablesHelper();

        /// <summary>
        /// 使用模板策略，处理DataTables类的信息
        /// </summary>
        public override void Process()
        {
            //因为前台有Get和Post方式，都可以使用Params这个方法取得
            this.sEcho = Context.Request.Params["sEcho"];
            this.iColumns = ParseHelper.Parse<int>(Context.Request.Params["iColumns"]);
            this.sColumns = ParseHelper.Parse<string>(Context.Request.Params["sColumns"]);
            this.iDisplayStart = ParseHelper.Parse<int>(Context.Request.Params["iDisplayStart"]);
            this.iDisplayLength = ParseHelper.Parse<int>(Context.Request.Params["iDisplayLength"]);
            this.bRegex = ParseHelper.Parse<bool>(Context.Request.Params["bRegex"]);
            this.sSearch = ParseHelper.Parse<string>(Context.Request.Params["sSearch"]);

            this.iSortingCols = ParseHelper.Parse<int>(Context.Request.Params["iSortingCols"]);
            this.iSortCol_0 = ParseHelper.Parse<int>(Context.Request.Params["iSortCol_0"]);
            this.sSortDir_0 = ParseHelper.Parse<string>(Context.Request.Params["sSortDir_0"]);
            //this.bSortable_0 =ParseHelper.Parse<bool>( Context.Request.Params["bSortable_0"]);

            ProcessPageInfo();
            PageInfo.sEcho = string.IsNullOrEmpty(PageInfo.sEcho) ? "success" : PageInfo.sEcho;
            Context.Response.ContentType = "text/plain";
            Context.Response.Write(PageInfo.ToString());
        }

        /// <summary>
        /// 处理数据分页信息
        /// </summary>
        public virtual void ProcessPageInfo()
        {
            if (!string.IsNullOrEmpty(this.sSearch))
            {
                doSearch();
            }
            else if (this.iSortingCols > 0)
            {
                doSort();
            }
            else
            {
                doQuery();
            }
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        public virtual void doQuery()
        {
            PageInfo.aaData = new List<List<object>>();
        }

        /// <summary>
        /// 搜索方法
        /// </summary>
        public virtual void doSearch()
        {
            PageInfo.aaData = new List<List<object>>();
        }

        /// <summary>
        /// 排序
        /// </summary>
        public virtual void doSort()
        {
            PageInfo.aaData = new List<List<object>>();
        }

    }
}